// packageG/mycomponent/taskPackage/taskPackage.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    index: {
      type: null,
    },
    U_tabcontrol: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    isLast: {
      type: null,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    info: [],
    showShare: false,
    options: [{
        name: "视频",
        icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FtaskPackage%2Fshipin.png",
        router: "VideoList"
      },
      {
        name: "文章",
        icon: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FtaskPackage%2Fwenzhang.png",
        router: "notice"
      }
    ],
    taskDay: "",
    taskID: "",
    usable_name: '',

    page: 1,
    isLoadMore: true,
    total_page: 0
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.getData();
    },
    moved() {},
    detached() {},
  },
  observers: {
    datas:function(){
      this.getData();
    },
    bottomShowBol: function (flag) {
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore) {
        this.getMoreData();
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showShareCancel() {
      this.setData({
        showShare: false
      })
    },
    makeTask(e) {
      let items = e.currentTarget.dataset.item;
      this.data.taskID = items.id;
      this.data.showShare = true;
      let currentDate = new Date();
      let currentYear = currentDate.getFullYear();
      let currentMonth = currentDate.getMonth() + 1;
      let currentDay = currentDate.getDate();
      this.data.taskDay = `${currentYear}-${currentMonth}-${currentDay}`;
      this.setData({
        taskID: this.data.taskID,
        showShare: this.data.showShare,
        taskDay: this.data.taskDay
      })
    },
    shareHandle(e) {
      let item = e.currentTarget.dataset.item;
      if (item.router == "VideoList") {
        wx.navigateTo({
          url: `/packageC/video_goods/VideoList/VideoList?taskID=${this.data.taskID}&taskDay=${this.data.taskDay}`,
        })
      } else {
        wx.navigateTo({
          url: `/packageA/member/notice/notice?taskID=${this.data.taskID}&taskDay=${this.data.taskDay}`,
        })
      }
    },
    getMoreData() {
      this.data.isLoadMore = false;
      if (this.data.page >= this.data.total_page) {
        return;
      }
      this.setData({
        page: this.data.page + 1
      })
      app._getNetWork({
        url: app.getNetAddresss("plugin.task-package.frontend.daily.decorate-list"),
        data: {
          page: this.data.page,
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              info: this.data.info.concat(res.data.data),
              total_page: res.data.last_page,
              isLoadMore: true
            })
            if (this.data.page >= this.data.total_page) {
              this.setData({
                isLoadMore: false
              })
            }
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    getData() {
      app._getNetWork({
        url: app.getNetAddresss("plugin.task-package.frontend.daily.decorate-list"),
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              info: res.data.data,
              usable_name: res.data.usable_name,
              total_page: res.data.last_page,
              isLoadMore: true
            })
            if (this.data.page >= this.data.total_page) {
              this.setData({
                isLoadMore: false
              })
            }
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
})